package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.entity.resource.Customer;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectItemResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.customer.CustomerAddReqVo;
import com.zmj.sy.mom.srv.aps.mapper.CustomerMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class CustomerService {

    private final CustomerMapper customerMapper;

    @Transactional
    public void add(CustomerAddReqVo reqVo) {
        Customer customer = MkBeanUtils.copyProperties(reqVo, Customer.class);
        customer.setMyCode(reqVo.getCode());
        customerMapper.insert(customer);
    }


    public BaseSelectResVo select() {
        List<Customer> customers = customerMapper.selectList(null);
        List<BaseSelectItemResVo> baseSelectItemResVoStream = customers.stream()
                .map(e -> {
                    BaseSelectItemResVo r = new BaseSelectItemResVo();
                    r.setText(e.getName());
                    r.setValue(e.getId());
                    r.getAttr().put("myCode", e.getMyCode());
                    return r;
                })
                .collect(Collectors.toList());

        return BaseSelectResVo.of(baseSelectItemResVoStream);
    }
}
